<html>
	<head>
		<title>Install CloudCMS</title>
		<link rel="stylesheet" href="./admin/style/style.css" type="text/css">
	</head>
	<body>
		<div id="header">
		</div>
		<div id="container">
			<div id="content">
				<h2>Install CloudCMS</h2>
				<?php
				if (isset($_POST['submit']))
				{
					$title        = $_POST['site_title'];
					$adm_username = $_POST['username'];
					$adm_password = md5($_POST['password']);
					
					if (is_writable('./config.php'))
					{
						$fh = fopen('./config.php', 'w');
						fwrite($fh, '<?php $site_title = "' . $title . '"; $default_page = "1Home"; $default_template = "rounded"; $admin_username = "' . $adm_username . '"; $admin_password = "' . $adm_password . '"; ?>');
						fclose($fh);
					}
					else
					{
						die('Config.php is not writeable or does not exist.');
					}
					
					if (is_writable('./pages/'))
					{
						$fh = fopen('./pages/1Home', 'w');
						fwrite($fh, '<p>If you can see this, it probably means your installation of CloudCMS was successful. You can now log into the <a href="./admin/">Admin Panel</a> and start managing your website. For security, please remove install.php.</p>');
						fclose($fh);					
					}
					else
					{
						die('The directory "pages/" is not writable or does not exist.');
					}
					?>
					<meta http-equiv="refresh" content="0;url=./index.php" />
					<?php
					exit;
				}
				else
				{
				?>
				<p>In order to use CloudCMS, the directory "pages" must be writable, as CloudCMS uses files to store pages, rather than a database. Appropriate permissions would be '777'. As well as the pages directory the installation must write to the configuration file in the Admin directory, this file is called 'config.php' and is in the folder 'admin'.</p>
				<form action="install.php" method="post">
					<strong>Site Title:</strong><br />
					<input type="text" name="site_title" style="font-family: Verdana; font-size: 12px; width: 300px;"><br /><br />
					<strong>Admin Username:</strong><br />
					<input type="text" name="username" style="font-family: Verdana; font-size: 12px; width: 300px;"><br /><br />
					<strong>Admin Password:</strong><br />
					<input type="password" name="password" style="font-family: Verdana; font-size: 12px; width: 300px;"><br /><br />
					<?php
					if (!is_writable('./config.php'))
					{
					?>
					<strong><div style="color: red;">config.php - Not Writable</div></strong>
					<?php
					}
					else
					{
					?>
					<strong><div style="color: green;">config.php - Writable</div></strong>
					<?php
					}
					?>
					<br />
					<?php
					if (!is_writable('./pages/'))
					{
					?>
					<strong><div style="color: red;">pages/ - Not Writable</div></strong>
					<?php
					}
					else
					{
					?>
					<strong><div style="color: green;">pages/ - Writable</div></strong>
					<?php
					}
					?>
					<br />
					<input type="submit" name="submit" value="Install">
				</form>
				<?php
				}
				?>
			</div>
		</div>
	</body>
</html>